/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package SteamEngineFX.Layouts;

import SteamEngineDB.Schedule;
import SteamEngineFX.data.ScheduleController;
import SteamEngineFX.data.SteamEngineConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.Cursor;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.joda.time.LocalDate;

/**
 *
 * @author jbendixsen
 */
public class ApplicationPane extends BorderPane{
    
    //The MainPane has 4 main sections
    private TopPane topPane = null;
    private StatusBarPane statusBarPane = null;
    private NavigationPane navPane = null;
    private WorkAreaPane workAreaPane = null;
    private PropertiesPane propertiesPane = null;
    
    private Application steamEngineInstance = null;
    private Stage primaryStage = null;
    private SteamEngineConnection connection = null;
    
    private ScheduleController scheduleController = null;
    private List<Integer> scheduleHistory = new ArrayList<Integer>();
    private Schedule currentSchedule = null;
    
    private LocalDate selectedDate = new LocalDate();
    
    private void setCurrentSchedule(Schedule schedule){
        currentSchedule = null;
        if (schedule != null){
            currentSchedule = schedule;
            scheduleHistory.add(currentSchedule.getId());
        }
    }
    public Schedule getCurrentSchedule(){
        return currentSchedule;
    }
    
    public Application getAppInstance(){
        return steamEngineInstance;
    }
    
    public Stage getPrimaryStage(){
        return primaryStage;
    }
    
    public ApplicationPane(Application SteamEngineInstance, Stage PrimaryStage, SteamEngineConnection steamEngineConnection){
        
        steamEngineInstance = SteamEngineInstance;
        primaryStage = PrimaryStage;
        connection = steamEngineConnection;
        
        scheduleController = new ScheduleController(connection);

        topPane = new TopPane(this, primaryStage);
        statusBarPane = new StatusBarPane();
        navPane = new NavigationPane(this);
        //TODO: bind the work pane to the anvigator property
        workAreaPane = new WorkAreaPane();
        propertiesPane = new PropertiesPane(this);

        
        setTop(topPane);
        setBottom(statusBarPane);
        setLeft(navPane);
        setCenter(workAreaPane);
        setRight(propertiesPane);
        
        propertiesPane.refresh();
        
    }
    
//    private ObjectProperty<Date> selectedDate = new SimpleObjectProperty<Date>();
    
    public void initSelectedDate(LocalDate date){
        topPane.initSelectedDate(date);
//        this.setSelectedDate(date);
    }
    
    public void reloadSchedule(){
        
    }

    //this gets call from TopPane when the user changes the date
    public void setSelectedDate(LocalDate date) {
        
        //do nothing if the date hasn't changed
        if (date != selectedDate){
                
            this.setCursor(Cursor.WAIT);
            try{
                //if the date has changed then we get the new schedule
                //this will create the schedule if it doesn't exist
                Schedule s = scheduleController.loadSchedule(date);
                if (s != null){
                    //set cerrentSchedule and history
                    setCurrentSchedule(s);
                    navPane.LoadSchedule(s);
                    
                }
            }finally{
                this.setCursor(Cursor.DEFAULT);
            }
        }
    }

//    public Date getSelectedDate() {
//        return selectedDate.get();
//    }

    /**
     * 
     * @param NewStatus 
     */
    public void setStatus(String NewStatus){
        statusBarPane.setStatusLabel(NewStatus);
    }
        
    public void setDebug(String DebugText){
        statusBarPane.setDebugText(DebugText);
    }
        

}
